import { getToken,setCookie } from "@/utils/token"
import {getLoginUserInfo, login} from "@/api/sysAuth";
import config from '@/enum/setting.js'
import store from "..";
export const userStore={
    state:() =>({
         // 用户信息
         user: {},
         // 登录token
         token: getToken(),
         // 用户角色
         roles: [],
         // 路由加载
         menuFlag: false
    }),
    getters:{
        user: state => state.user,
        roles: state => state.roles,
        token: state => state.token,
        menuFlag: state => state.menuFlag,
    },
    actions:{
// 解析加载路由
        loadMenus({commit}) {
            return new Promise((resolve, reject) => {
                commit('SET_MENU', true)
            })
        },
        // 登出
        LogOut({commit}) {
            return new Promise((resolve, reject) => {
                resolve('登出模拟')
            })
        },
        // 登录
        login({commit}, newUser) {
            return new Promise((resolve, reject) => {
                login(newUser).then(res => {
                    // 登录成功,拼接token
                    let value = res.value
                    let token = "Bearer" + " " + value
                    // token 存储
                    setCookie(config.tokenCookieKey, token)
                    setToken(token, commit)
                    // 设置路由未加载
                    updateMenu(true, commit)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 获取用户信息
        saveUserCache({commit}) {
            return new Promise((resolve, reject) => {
                getLoginUserInfo().then(res => {
                    if (res.state) {
                        saveUserInfo(res.data,commit)
                        resolve(res.data)
                    } else {
                        reject('用户信息获取异常')
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        }
    },
    mutations:{
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_USER: (state, user) => {
            state.user = user
        },
        SET_MENU: (state, menuFlag) => {
            state.menuFlag = menuFlag
        },
        SET_TOKEN: (state, value) => {
            state.token = value
        }
    }
}

/**
 * 定义一些存储数据的方法
 */
/**
 * 存储客户信息到store中
 */
export const saveUserInfo = (res, commit) => {
    let user = res.sysUser
    let roles = res.authorities
    // 给个默认值
    let newRole = [];
    if (roles == null || roles.length === 0) {
        newRole = ['ROLE_SYSTEM_DEFAULT']
        commit('SET_ROLES', newRole)
        commit('SET_USER', user)
    } else {
        for (let role of roles) {
            newRole.push(role.authority)
        }
        commit('SET_ROLES', newRole)
        commit('SET_USER', user)
    }
}

/**
 * 路由解析
 */
export const updateMenu = (res, commit) => {
    commit('SET_MENU', res)
}


export const setToken = (res, commit) => {
    commit('SET_TOKEN', res)
}